package com.ctgu.tengyun.controller.product;

import com.ctgu.tengyun.bean.convert.ProductInfoConvert;
import com.ctgu.tengyun.bean.errorcode.CommonErrorCode;
import com.ctgu.tengyun.bean.request.SaveProductInfoRequest;
import com.ctgu.tengyun.bean.response.ProductDetail;
import com.ctgu.tengyun.bean.response.ProductListInfoResponse;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.exception.ErrorCodeException;
import com.ctgu.tengyun.service.product.ProductInfoService;
import com.ctgu.tengyun.util.ParameterTool;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/7/17.
 */
@RestController
@RequestMapping(value = "/v1/product/")
public class ProductInfoController {

    @Resource
    private ProductInfoService productInfoService;

    @GetMapping(value = "list_info")
    public ProductListInfoResponse listInfo(HttpServletRequest request) {
        Integer pageNo = ParameterTool.getParameterInteger(request, "pageNo", 1);
        Integer pageSize = ParameterTool.getParameterInteger(request, "pageSize", 10);
        String productNo = ParameterTool.getParameterString(request, "productNo", "");
        String productName = ParameterTool.getParameterString(request, "productName", "");

        Validator.validateGt(pageNo, 0);
        Validator.validateBetweenIn(pageSize, 0, 1000);

        Integer totalCount = productInfoService.countProduct(productNo, productName);
        List<ProductInfo> productInfoList = Collections.emptyList();
        if (totalCount > 0) {
            productInfoList = productInfoService.selectProduct(pageNo, pageSize, productNo, productName);
        }

        return ProductInfoConvert.toProductListInfoResponse(totalCount, pageSize, productInfoList);
    }

    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public ProductDetail detail(@PathVariable("id") Integer id) {
        ProductInfo productInfo = productInfoService.selectByPrimaryKey(id);
        return ProductInfoConvert.toProductDetail(productInfo);
    }

    @RequestMapping(value = "save_or_update", method = RequestMethod.POST)
    public Object save(@RequestBody SaveProductInfoRequest request) {
        ProductInfo productInfo = ProductInfoConvert.fromSaveProductInfoRequest(request);

        if (null != productInfo.getId()) {
            ProductInfo realProduct = productInfoService.selectByPrimaryKey(productInfo.getId());
            Validator.assertResourceFound(null != realProduct, "product not found, id:" + productInfo.getId());
            productInfo.setVersion(realProduct.getVersion());
            productInfoService.updateByPrimaryKeySelective(productInfo);
        } else {
            try {
                productInfoService.insertSelective(productInfo);
            } catch (DuplicateKeyException e) {
                throw new ErrorCodeException(CommonErrorCode.duplicate_add, CommonErrorCode.duplicate_add.getMsg() + productInfo.getProductNo());
            }
        }

        return ImmutableMap.of("id", productInfo.getId());
    }

}